<?php

class MyCenter extends CI_Controller {
	
 

	public function __construct()
	 {
	  	parent::__construct();
	  	date_default_timezone_set('PRC');

	 }
 
	public function index()  
	{	
		$this->order_list();
	}
	
	public function readUserIdFromSession(){
		
		$whereUserid = "and userId=-1";
		if($this->session->userdata('user_in'))
	 		$whereUserid = " and userId=".$this->session->userdata('user_id');
	 	return $whereUserid;
	}
	/**
	 * 
	 * 我的订单
	 */
	public function order_list()  
	{
		$orderList="";
		$item[0]='我的订单';
		$url = base_url()."myCenter/order_list";
		$data['url'] = $url;
		$data['item']=$item;
		$tStart = date('Y-m-d H:i:s',strtotime("-30 day 00:00:01"));  // 默认最近一个月
		$tStart = date('Y-m-d H:i:s',strtotime("1970-01-01 00:00:01")); // 默认显示所有
		$tEnd = date('Y-m-d H:i:s',strtotime("+1 day 00:00:01"));
		$whereTime = " and orderdetail.ctime>='".$tStart."' and orderdetail.ctime<='".$tEnd."'";
		
		$this->load->model('mycenter_model');
		$whereUserid = $this->readUserIdFromSession();
		$orderList = $this->mycenter_model->readOrderList($whereTime, "", $whereUserid);

		//$orderListHtml = $this->genOrderListTable($orderList);
		//$data['orderListHtml'] = $orderListHtml;
		$data['left_tab'] = 'order_list';
		$data['orderList'] = $orderList;
		$this->load->view('myCenter/myCenter_home',$data);
	}
	
	/**
	 * 可以定制查询多长时间内的订单,比如一个月之内,半年之内,或者全部
	 * 
	 */
	public function order_list_ajax() {

		$index = $_POST['selectValue'];
		if($index <= 0)
			$t = "-30 day 00:00:01";
		if($index == 1)
			$t = "-60 day 00:00:01";
		if($index == 2)
			$t = "-180 day 00:00:01";
		if($index >= 3)
			$t = "1970-01-01 00:00:01";

		$tStart = date('Y-m-d H:i:s',strtotime($t));
		$tEnd = date('Y-m-d H:i:s',strtotime("+1 day 00:00:00"));
		$whereTime = " and orderdetail.ctime>='".$tStart."' and orderdetail.ctime<='".$tEnd."'";
		$this->load->model('mycenter_model');

		$whereUserid = $this->readUserIdFromSession();
		$orderListObj = $this->mycenter_model->readOrderList($whereTime, "",$whereUserid);
		
		$orderListHtml = $this->genOrderListTable($orderListObj['orderList']);
	    
	    $jsonA['html'] = $orderListHtml;
	    $jsonA['number'] = $orderListObj['number'];
		echo json_encode($jsonA);
	    
	}

	
	public function address_management()  
	{
	}
	public function password_management()  
	{
		$item[0]='更改密码';
		$url = base_url()."myCenter/password_management";
		$data['url'] = $url;
		$data['item']=$item;
		$data['left_tab'] = 'password_management';
		$this->load->view('myCenter/myCenter_home',$data);
	}
}

?>